package test;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class Mp {

	public static void main(String[] args) {

		//		1.   pom.xml需要加此坐标
		//		<!-- 模板引擎 -->
		//		<dependency>
		//		    <groupId>org.apache.velocity</groupId>
		//		    <artifactId>velocity-engine-core</artifactId>
		//		    <version>2.1</version>
		//		</dependency>

		//        2.先创建放XxxMapper.xml的文件夹		




		// 代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		final String projectPath = System.getProperty("user.dir");//获取当前项目物理路径
		gc.setOutputDir(projectPath + "/src/main/java");
		gc.setAuthor("dede");//设置作者名
		gc.setOpen(false);	//生成后是否打开文件
		gc.setEnableCache(false);// XML 二级缓存	需要ehcache的maven坐标
		gc.setFileOverride(true);//生成的文件覆盖旧文件
		gc.setBaseResultMap(true);//生成BaseResultMap
		gc.setBaseColumnList(true);//生成BaseColumnList
		gc.setActiveRecord(true);//开启ActiveRecord模式
		gc.setIdType(IdType.AUTO);//设置主键生成策略
		mpg.setGlobalConfig(gc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:mysql://localhost:3306/powersystemdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
		// dsc.setSchemaName("public");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("569486365");
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		//自定义模块名
		final String moduleName="bdqn";
		pc.setModuleName(moduleName);
		pc.setParent("com");//==== 包名（自己手动设置）
		mpg.setPackageInfo(pc);

		// 自定义配置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};
		List<FileOutConfig> focList = new ArrayList();
		focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输入文件名称================================模块名（自己设置）
				return projectPath + "/src/main/resources/mapper/"   
				+ tableInfo.getEntityName() + "Mapper.xml" ;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);
		mpg.setTemplate(new TemplateConfig().setXml(null));

		// 策略配置**（个性化定制）**
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);

		//实体类自动继承Entity,不需要也可以
		//strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");

		strategy.setEntityLombokModel(false);//配置实体类是否开启小辣椒(Lombok)
		strategy.setRestControllerStyle(true);
		//控制层自动继承父类BaseController,不需要也可以
		//strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
		//设置要被扫描的表名,可传多个参数
		strategy.setInclude("ps_function","ps_inspectiontask_detail","ps_inspectiontask_main","ps_inspector_detail",
				"ps_line_info","ps_logs","ps_pole_info","ps_role","ps_role_premission",
				"ps_solver_detail","ps_solvetask_detail","ps_solvetask_main","ps_systemconfig","ps_user");

		mpg.setStrategy(strategy);
		mpg.execute();
		System.out.println("ok");

	}

}
